package ProjectManagement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.spm.pojo.Teacher;
/**
 *  数据库访问层-管理员的CRUD操作
 * @author DY1101shaoyuxian
 *
 */
public class TeacherDaoImpl {
    private Connection conn = DobConn.getConn();
    //登陆用户验证
    public boolean isLogin(Teacher teacher){
        boolean flag=false;
        String sql="select * from Teacher where Tid=? and Tpassword=?";
        try {
            PreparedStatement pmst=conn.prepareStatement(sql);
            pmst.setString(1, teacher.getTid());//对传过来的用户名和密码进行封装
            pmst.setString(2, teacher.getPassword());
            ResultSet rs=pmst.executeQuery();
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改用户的密码
     * @param teacher
     * @return
     */

    public int updateUserPassWord(Teacher  teacher) {
        int a=0;
        String sql = "update Teacher set Tpassword=? where Tid=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, teacher.getPassword());
            pstmt.setString(2, teacher.getTid());
            a= pstmt.executeUpdate();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return a;
    }
    public int addTeacher(Teacher teacher) {
        int i = 0;
        //sql语句
        String sql = "insert into users values(?,?,?,?)";

        try{
            //创建PreparedStatement对象
            PreparedStatement pmst = conn.prepareStatement(sql);
            // 为占位符传值
            pmst.setString(1, teacher.getTid());
            pmst.setString(2, teacher.getPassword());
            pmst.setString(3, teacher.getSex());
            pmst.setString(4, teacher.getSex());
            //执行sql语句
            i = pmst.executeUpdate();
            //释放资源
            pmst.close();
            conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return i;
    }
}

